package com.sz.stock.utils;

import com.sz.stock.commons.HttpClientUtil;
import com.sz.stock.model.Stock;
import com.sz.stock.service.IStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@EnableScheduling
public class MPriceTask {
    @Autowired
    private IStockService stockService;
    @Autowired
    private RedisUtil redisUtil;
    @Scheduled(cron="0 */1 * * * ?")
    //@Scheduled(fixedRate=5000)
    private void configureTasks(){
        System.err.println("执行静态定时任务："+ LocalDateTime.now());
        List<Stock> stocks = stockService.getAll();
        Map<String,Double> marketprice=new HashMap<>();
        String url = null;
        for (int i = 0; i < stocks.size(); i++) {
            //循环查询所有股票当前信息
            //从接口获取数据
            url = "http://qt.gtimg.cn/q=sz" + stocks.get(i).getStockCode();
            String method = HttpClientUtil.getMethod(url);
            String[] split = method.split("\"");
            String[] data = split[1].split("~");
            marketprice.put(stocks.get(i).getStockCode(),Double.parseDouble(data[3]));
        }
        redisUtil.set("marketPrice",marketprice);
    }
}
